﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using IndyMosaic.Core.Email;
using IndyMosaic.Models.Web;
using IndyMosaic.Models.Podcast;

namespace IndyMosaic.Org.Site.Controllers
{
    public class AdministrationController : Controller
    {
        private IPodcastRepository podcastRepository { get; set; }
        private INewsItemRepository newItemRepository { get; set; }
        private IMediaRepository mediaRepository { get; set; }

        public AdministrationController()
            : this(new NewsItemRepository(), new PodcastRepository(), new MediaRepository())
        { }
        public AdministrationController(INewsItemRepository newsItemRepository, IPodcastRepository podcastRepository, IMediaRepository mediaRepository)
        {
            this.newItemRepository = newsItemRepository;
            this.podcastRepository = podcastRepository;
            this.mediaRepository = mediaRepository;
        }

        //
        // GET: /Administration/
        [Authorize(Roles = "Administrator")]
        public ActionResult Index()
        {
            return View(); 
        }

        [Authorize(Roles = "Administrator")]
        public ActionResult LiveStream()
        {
            mediaRepository.ShowLiveStream(!mediaRepository.ShowingLiveStream());
            return Redirect("~/");
        }

        #region User Maintence
        
        [Authorize(Roles = "Administrator")]
        public ActionResult Users(bool? activeUsersOnly)
        {
            var users = Membership.GetAllUsers();

            return View(users);
        }
        
        [Authorize(Roles = "Administrator")]
        public ActionResult ResetUserPassword(Guid id)
        {
            //MembershipUser user = Membership.GetUser(id);
            var user = UserRepository.GetUser(id);
            string newPassword = user.MembershipUser.ResetPassword();
            Membership.UpdateUser(user.MembershipUser);
            string body = string.Format("Your password has been reset to: {0}/r/nDon't forget to change your password after you login.", newPassword);
            EmailUtility.SendEmail(EmailUtility.GetMailAddress(UserRepository.GetUser().MembershipUser, UserRepository.GetUser().Profile.FullName),
                EmailUtility.GetMailAddress(user.MembershipUser, user.Profile.FullName), "You password has been reset", body);
            return View();
        }

        [Authorize(Roles = "Administrator")]
        public ActionResult EditUser(Guid id)
        {
            var user = UserRepository.GetUser(id);
            return View(user);
        }

        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult EditUser(Guid id, FormCollection collection)
        {
            IndyMosaic.Models.Web.User user = UserRepository.GetUser(id);
            user.MembershipUser.Email = collection["MembershipUser.Email"];
            user.MembershipUser.IsApproved = collection["MembershipUser.IsApproved"].StartsWith("true");
            user.Profile.FullName = collection["Profile.FullName"];
            user.Profile.Save();
            if (Roles.GetRolesForUser(user.MembershipUser.UserName).Count() > 0)
            {
                Roles.RemoveUserFromRoles(user.MembershipUser.UserName, Roles.GetRolesForUser(user.MembershipUser.UserName));
            }
            foreach (string roleName in Roles.GetAllRoles())
            {
                if (collection[roleName] != null)
                {
                    if (collection[roleName].StartsWith("true"))
                    {
                        Roles.AddUserToRole(user.MembershipUser.UserName, roleName);
                    }
                }
            }

            Membership.UpdateUser(user.MembershipUser);
            return RedirectToAction("Users");
        }
        
        [Authorize(Roles = "Administrator")]
        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult Register(ViewData.Administration.RegisterUser regUser)
        {
            MembershipUser newUser = Membership.CreateUser(regUser.User.MembershipUser.Email, regUser.Password, regUser.User.MembershipUser.Email);
            var user = UserRepository.GetUser(newUser.UserName);
            user.Profile.FullName = regUser.User.Profile.FullName;
            user.Profile.Save();
            Roles.AddUserToRoles(user.MembershipUser.UserName, regUser.User.Roles);

            return View("Users", Membership.GetAllUsers());
        }

        #endregion

        #region News Item Maintence
        [Authorize(Roles = "Administrator")]
        public ActionResult NewsItems()
        {
            return View(newItemRepository.GetAll());
        }
        [HttpGet]
        [Authorize(Roles= "Administrator")]
        public ActionResult EditNewsItem(int id)
        {
            NewsItem ni = newItemRepository.GetById(id);
            if (ni == null)
            {
                return View("NotFound");
            }
            else
            {
                return View(ni);
            }
        }
        [HttpPost]
        [ValidateInput(false)]
        [Authorize(Roles = "Administrator")]
        public ActionResult EditNewsItem(NewsItem editedItem)
        {
            if (ModelState.IsValid)
            {
                NewsItem item = newItemRepository.GetById(editedItem.ID);
                item.Body = editedItem.Body;
                item.ExpirationDate = editedItem.ExpirationDate;
                item.Title = editedItem.Title;
                newItemRepository.Save();
            }
            else
            {
                return View(editedItem);
            }
            return View("NewsItems", newItemRepository.GetAll());
        }

        [Authorize(Roles="Administrator")]
        public ActionResult NewNewsItem()
        {
            return View();
        }
        #endregion

        #region Podcasts Maintence
        [Authorize(Roles="Administrator")]
        public ActionResult Podcasts()
        {
            return View(podcastRepository.ListAllEntries());
        }
        #endregion
    }
}
